/*
Online Java - IDE, Code Editor, Compiler
Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

//Form lines 240 onwards, there is no longer comparison with nums.length
//instead using Store[i].length to respect the placeholder to hold all the arrays with contiguous numbers


import java.util.*;

public class Main
{
    //TEST CASES*********************************************************************8
    //static int []nums = new int[]{100,4,200,1,2,6,7,8,9,100,101,102,103};
    //static int []nums = new int[]{100,4};
    //static int []nums = new int[] {1,2,3,4};
   //static int []nums = new int[] {8,1,6,2,9,7,3,100,4,5};
   //static int []nums = new int[] {10,11,12,50,51,52};
    //static int []nums = new int[]{1,1,1,1,1,1};
    //static int []nums = new int[]{100,4,200,1,0,3,2};
    
    //Test cases table (First tests)
    //static int []nums = new int[] {5};   //Issue if OPTION 2:  ArrayIndexOutOfBoundsException
    //static int []nums = new int[] {100,4};
    //static int []nums = new int[] {1,2,3,4};
    //static int []nums = new int[] {100,4,200,1,3,2};
    //static int []nums = new int[] {1,2,2,3};
    //static int []nums = new int[] {10,11,12,50,51,52};
    //static int []nums = new int[] {8,1,6,2,9,7,3,100,4,5};
    //static int []nums = new int[] {0,1,2,3};
    //static int []nums = new int[] {100,4,200,1,0,3,2};  //ISSUE if OPTION 2: it mishandles  {0,1,2,3,4} and {1,2,3,4} as same length
    //static int []nums = new int[] {-3,-2,-1,5,6};
    //static int []nums = new int[] {1,1,1,1};
    //static int []nums = new int[] {};  //Issue if OPTION 2:  ArrayIndexOutOfBoundsException
    //static int []nums = new int[] {1,10,2,20,3,30};
    //static int []nums = new int[] {5,6,7,50,51,52,53,1,2,3,4};
    //static int []nums = new int[] {9,1,4,7,3,2,6,5,8,8,2,1};
    //static int []nums = new int[] {0};
    //static int []nums = new int[] {0,0,0};
    //static int []nums = new int[] {-1,0,1,2};
    //static int []nums = new int[] {-2,-1,0,2,1,10};
    //static int []nums = new int[] {0,2,4,6};
    //static int []nums = new int[] {0,1,1,2,2,3,3,4};
    //static int []nums = new int[] {0,5,6,7,8};
    //static int []nums = new int[] {-5,-4,-3,-2,-1,0};
    //static int []nums = new int[] {0,1,2,50,51,52,53,54,55};
    
    
    //Additional “0 in the middle” torture tests
    //static int []nums = new int[] {2,3,4,0,1};
    //static int []nums = new int[] {-1,0,1,2,3};
    //static int []nums = new int[] {-1,0,1,2,100};
    //static int []nums = new int[] {0,-1,-2,-3};
    
    //Combined test suite
    //static int []nums = new int[] {-2,-1,0,1,2};
    //static int []nums = new int[] {-4,-3,-2,-1,0,1};
    //static int []nums = new int[] {-1,0,1,2,3,50};
    //static int []nums = new int[] {-3,-2,-1,0,1,2,100,101};
    //static int []nums = new int[] {0,0,1,2,3,0,4};
    //static int []nums = new int[] {0,2,0,1,0,3,0,4,0};
    //static int []nums = new int[] {0,1,2,2,3,3,4,0,0};
    //static int []nums = new int[] {0,100,0,101,0,102,0,103};
    //static int []nums = new int[] {0,5,6,7,8};
    //static int []nums = new int[] {2,3,4,0,1};
    //static int []nums = new int[] {0,-1,-2,-3};
    //static int []nums = new int[] {-10,-9,-8,0,1,2,3};
    //static int []nums = new int[] {-5,-4,-3,-2,-1,0};
    //static int []nums = new int[] {0,1,2,50,51,52,53,54,55
    //static int []nums = new int[] {0,1,3,4,5,6,7,8,9};
    //static int []nums = new int[] {1,1,2,2,3,3,4,4};
    //static int []nums = new int[] {5,5,5,4,4,3,3,2,2,1,1};
    //static int []nums = new int[] {7,1,9,2,8,3,10,4};
    //static int []nums = new int[] {1,100,2,200,3,300,4,400};
    //static int []nums = new int[] {0,1,2,3,4,5,6,7,8,9};
    
    //Test cases table (Further tests)
    //static int []nums = new int[] {-2,-1,0,1,2};
    //static int []nums = new int[] {-4,-3,-2,-1,0,1};
    //static int []nums = new int[] {-1,0,1,2,3,50};
    //static int []nums = new int[] {-10,-9,-8,0,1,2,3};
    //static int []nums = new int[] {-3,-2,-1,0,1,2,100,101};
    //static int []nums = new int[] {0,0,1,2,3,0,4};
    //static int []nums = new int[] {0,1,2,2,3,3,4,0,0};
    //static int []nums = new int[] {0,100,0,101,0,102,0,103};
    //static int []nums = new int[] {1,1,2,2,3,3,4,4};
    //static int []nums = new int[] {5,5,5,4,4,3,3,2,2,1,1};
    //static int []nums = new int[] {10,10,10,11,11,12,12,13};
    //static int []nums = new int[] {1,2,3, 10,11,12, 20,21,22};
    //static int []nums = new int[] {50,51,52,53, 1,2,3,4};
    //static int []nums = new int[] {8,9,10, 1,2,3,4,5, 100};
    //static int []nums = new int[] {1,100,2,200,3,300,4,400};
    //static int []nums = new int[] {7,1,9,2,8,3,10,4};
    //static int []nums = new int[] {-8,-7,-6,-5,-4};
    //static int []nums = new int[] {-1,-1,-2,-2,-3,-3};
    //static int []nums = new int[] { 15, 3, 2, 1, 6, 5, 4, 9, 8, 7, 12, 11, 10, 14, 13 };
    //static int []nums = new int[] { 100, 4, 200, 1, 3, 2, 5, 6, 7, 8, 9, 10 };
    //static int []nums = new int[] {2,1};
    //static int []nums = new int[] {2,3,4,5,1};
    //static int []nums = new int[] {-2,-1,0,1,2};
    //static int []nums = new int[] {-6,3,-1,7,-3,2,1,0,-4,-5,5,4,-2,6,8};
    //static int []nums = new int[] {-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,50,51,52};
    //static int []nums = new int[] {-2,-1,0,1,2,2,3,3,4,5,100,101,102,103,104,105};
    //static int []nums = new int[] {0,-1,-2,-3,-4,-5,10,11,12,13,14,15,16,17,100};
    
    //static int []nums = new int[] {30,31,32,33,34,35,36,37,38,39,40,-1,0,1};
    //static int []nums = new int[] {1,2,2,3,4,4,5,6,7,8,9,-1,0,-2,-3};
    //static int []nums = new int[] {0,0,0,-1,-2,-3,-4,1,2,3,4,5,6};
    //static int []nums = new int[] {-9,-8,-7,-6,-5,-4,-3,-2,-1,0,20,21,22,23,24,25,26,27};
    //static int []nums = new int[] {-100,-99,-98,-97,-96,0,1,2,3,4,5,6,7,8,9,10};
    
    //final tough
    //static int []nums = new int[] {0,0,-2,-1,0,1,2,0, 10,11,12,13,14,0, 50,51,52,53,54,0};
    //static int []nums = new int[] {0,-3,-2,-1,0,1,2,0, 20,21,22,23,24,25,0, 100,101,102,103,104,105,0,0};
    //static int []nums = new int[] {0,-2,-1,0,1,2,3,0, 50,51,52,53,54,0, 100,101,102,103,104,0};
    //static int []nums = new int[] {0,0,1,2,3,4,0, -5,-4,-3,-2,-1,0, 10,11,12,13,14,0, 100,101,102,103,104,0};
    //static int []nums = new int[] {0, -4,-3,-2,-1,0,0, 30,31,32,33,34,0, 60,61,62,63,64,0};
    //static int []nums = new int[] {0,0,1,2,3,4,5,0, 20,21,22,23,24,25,0, -10,-9,-8,-7,-6,-5,0,0};
    //static int []nums = new int[] {0,100,101,0,102,103,104,0, -1,0,1,2,3,0, 200,201,202,203,204,0};
    //static int []nums = new int[] {0,1,2,3,4,0,0, -2,-1,0,1,2,0, 10,11,12,13,14,0};
    //static int []nums = new int[] {0, -6,-5,-4,-3,-2,-1,0, 0,1,2,3,4,5,0, 50,51,52,53,54,55,0, 100,101,102,103,104,105,0};
    
    //Test case literal	Negative 		Positive or  negative  to 0
    //static int []nums = new int[] {-9,-8,-7,-6,-5,-4,-3,-2,-1,0, 20,21,22,23,24,25,26,27};
    //static int []nums = new int[] {-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0, 30,31,32,33,34,35,36,37};
    //static int []nums = new int[] {-7,-6,-5,-4,-3,-2,-1,0, 100,101,102,103,104,105,106,107};
    //static int []nums = new int[] {-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0, 1,2,3,4,5,6,7,8,9};
    //static int []nums = new int[] {-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0, 40,41,42,43,44,45};
    //static int []nums = new int[] {-6,-5,-4,-3,-2,-1,0, 10,11,12,13,14,15, 200,201};
    //static int []nums = new int[] {-8,-7,-6,-5,-4,-3,-2,-1,0, 50,51,52,53,54,55,56,57,58};
    //static int []nums = new int[] {-5,-4,-3,-2,-1,0, 1,2,3,4,5, 100,101,102,103,104};
    //static int []nums = new int[] {-9,-8,-7,-6,-5,-4,-3,-2,-1,0, 70,71,72,73,74,75,76,77, 0,0};
    //static int []nums = new int[] {0, -11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1, 90,91,92,93,94,95,96};
    
    //ULTIMATE test
    static int []nums = new int[] { 0,0, 50,51,52,53,54,55, -12,-11,-10,-9,-8,-7, 10,11,12,13,14,15, 100,101,102,103,104,105, -2,-1,0,1,2,3, 0,0,0, 200,300,400,-100,-50,999, 7,8,17,18,70,72,90,92, 12,52,101,-10,55,105, 0,0, 250,260,-200,33,35, 0,0,0 };
    
    
    //Using above three torture test cases to see if both options for the if loop are ok
    //it relates to section where it decides on storing sequence if it equal to existing in storeMaxConsecutiveSequences
    //static int []nums = new int[] {0,0,1,2,3,0,4};
    //static int []nums = new int[] {0,2,0,1,0,3,0,4,0};
    //static int []nums = new int[] {2,3,4,0,1};
    
    
    static int[][] Store = new int[nums.length][nums.length];
    
    static int [][][] storeMaxConsecutiveSequences = new int [nums.length][1][nums.length];
    
    static int differenceCheck;
    static int count=0;
    static int firstNum;
    static int nextNum;
    
    static int countSequencesStored;
    
    public static boolean nextnumbercheck(int firstNum)
    {
        for (int i=0; i<nums.length; i++)
        {
            if (nextNum==nums[i])
            {
                count++;
                Store[firstNum][count]=nextNum;
                return true;
            }
        }
        count=0;
        return false;
    } //end of method
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        boolean nextnumberconsecutive=false;
        
        for (int i=0; i< nums.length; i++)
        {
            Store[i][count]=nums[i];
            System.out.println("\nThis is the number being checked: " + nums[i]);
            differenceCheck=1;
            
            for (int j=0; j<nums.length; j++)
            {
                if (j==i) 
                {
                    j++;
                }
                if (j!=nums.length)
                {
                    if (nums[j]==nums[i]+differenceCheck)
                    {
                        count++;  
                        do
                        {
                            System.out.println("Next consecutive number has appeared: " + (nums[i]+differenceCheck));
                            Store[i][count]= nums[i]+differenceCheck;
                            differenceCheck++;
                            nextNum = nums[i]+differenceCheck;  //next consecutive number expected....
                            nextnumberconsecutive = nextnumbercheck(i);
                        }while(nextnumberconsecutive);
                        
                    }
                }
            }  //end of inner for loop
        }  //end of for loop going through all nums
   
        System.out.println("\n\nLength of store: " + Store.length);
        System.out.println("********These are all entries*********");
        
        for (int max[]: Store)   
        {
            System.out.println(Arrays.toString(max));
        }
        
        StringJoiner sj = new StringJoiner(",");
        int currentMaximumConsectiveNumbers=0;
        String numtoString;
        
        int currentI=0;
        int j=0;
        int zeroFoundLocation=0;
        boolean zeroFound=false;
        int posZero=0;
        
        int indexZeroElementInArray=0;
        int index;
        
        String backupStringJoiner;
        int countIndexLocationsNoZero=0;
        
        int [] currentConsecutiveElements = new int[nums.length];
        
        int captureIndex=0;
        
        int backupPosZero=0;
        
        boolean hasExecuteOnce=false;
        boolean hasZeroFound=false;
        
        
        for (int i=0; i<Store.length; i++)
        {
            System.out.println("\n******NEW NUMBER*****:  " + Arrays.toString(Store[i]));
            System.out.println("This is current highest consecutive Numbers in sequence stored: " + currentMaximumConsectiveNumbers);
            
            hasExecuteOnce=false;
            
            backupPosZero=posZero;
            
            captureIndex=0;
            countIndexLocationsNoZero=0;
            index=0;
            
            for (j=0; j<Store[0].length; j++)
            {
                int elementInArray = Store[i][j];
                
                zeroFound=false;
                
                currentConsecutiveElements[index] = elementInArray;
                
                index++;
                
                if (elementInArray==0)
                {
                    System.out.println("Zero found at index: " + j  + " => " + Store[i][j]);
                    
                    indexZeroElementInArray = j;
                    
                    captureIndex = j;
                    hasZeroFound=true;
                        
                    if (captureIndex>=posZero)
                    {
                        posZero=captureIndex;
                    }
                        
                    
                    zeroFoundLocation=j;
                    
                    zeroFound=true;
                    
                    if (j+1<Store[i].length-1)
                    {
                        if (j!=0 && Store[i].length!=1 && Store[i][j+1]==0)
                        {
                            j=Store[i].length;
                        }
                    }
                    
                    if (j-1>=0)
                    {
                        if (Store[i][j-1]==0 && hasZeroFound && j==(Store[i].length-1))
                        {
                            if (!hasExecuteOnce)
                            {
                                captureIndex=j-1;
                                posZero=posZero-1;
                                zeroFound=false;
                                hasExecuteOnce=true;
                            }
                        }
                    }
                }
                else
                {
                    System.out.println("222No zero found (index: " + j+") " + " => " + Store[i][j]);
                    
                    if (j>=posZero)
                    {
                        posZero=j;
                    }
                    
                    countIndexLocationsNoZero++;
                    zeroFound=false;
                }
                
                if (posZero>backupPosZero && posZero>captureIndex && (j==Store[i].length-1 |j==Store[i].length))
                {
                    posZero=captureIndex;
                }
                
                int lastElementIndex=0;
                
                if (j>Store[i].length-1)
                {
                    lastElementIndex=j-1;
                }
                else if (j==Store[i].length-1)
                {
                    lastElementIndex=j;
                }
                
                boolean hasMoreConsecutive=false;
                
                try
                {
                    if (Store[i][j+1]!=0)
                    {
                        hasMoreConsecutive=true;
                    }
                }
                catch (ArrayIndexOutOfBoundsException s)
                {
                    if (countIndexLocationsNoZero>currentMaximumConsectiveNumbers || posZero>currentMaximumConsectiveNumbers)
                    {
                        if (j<Store[i].length-1)
                        {
                            if (Store[i][j+1]==0)
                            {
                                System.out.println("1Zero found at index: " + (j+1)  + " => " + Store[i][Store[i].length-1]);
                                countIndexLocationsNoZero=j+1;
                                
                                j++;
                            }
                            else
                            {
                                System.out.println("No zero found (index: " + (j+1)+") " + " => " + Store[i][j+1]);
                                countIndexLocationsNoZero++;
                                j++;
                            }
                        }
                        
                        System.out.println("This is current highest streak of consecutiveNumbers: " + currentMaximumConsectiveNumbers);
                    
                        currentMaximumConsectiveNumbers=posZero;
                    
                        if (j==0 && Store[i].length==0)
                        {
                             currentMaximumConsectiveNumbers=1;
                        }
        
                        if (captureIndex>currentMaximumConsectiveNumbers)
                        {
                            currentMaximumConsectiveNumbers=captureIndex;
                        }
                        
                        if (countIndexLocationsNoZero>currentMaximumConsectiveNumbers)
                        {
                            currentMaximumConsectiveNumbers=countIndexLocationsNoZero;
                        }
        
                        if (Store[i][0]==0 && countIndexLocationsNoZero==Store[i].length-1)
                        {
                            currentMaximumConsectiveNumbers++;
                        }
        
                        if (j!=Store[i].length)
                        {
                            if (Store[i][j]==0 && countIndexLocationsNoZero==Store[i].length-1 
                            && (Store[i][j-1]+1)==Store[i][j])
                            {
                                currentMaximumConsectiveNumbers++;
                            }
                        }
        
                        if (j==currentMaximumConsectiveNumbers && Store[i][j]!=0)
                        {
                            currentMaximumConsectiveNumbers++;
                        }
        
                        System.out.println(lastElementIndex);
                        
                        System.out.println(elementInArray);
                        
                        System.out.println(Store[i][indexZeroElementInArray]);
                    
  
                        for (int k: nums)
                        {
                            if (k==0 && Store[i][0]!=0 && ((Store[i][posZero-1]+1)==elementInArray))
                            {
                                System.out.println("consecutive in negative direction");
                                currentMaximumConsectiveNumbers=currentMaximumConsectiveNumbers+1;
                
                                for (int g=0; g<indexZeroElementInArray;g++)
                                {
                                    if (Store[i][g]==0)
                                    {
                                        System.out.println("Earlier 0 at index:  " + g);
                                        System.out.println("Sequence negative => positive");
                                        currentMaximumConsectiveNumbers=currentMaximumConsectiveNumbers-1;
                                        break;
                                    }
                                }
                                break;
                            }
                        }
        
                        if (Store[i].length>(j+1)+1)
                        {
                            System.out.println("0 in original dataset");
                            currentMaximumConsectiveNumbers=currentMaximumConsectiveNumbers+1;
                        }
                        
                        if(Store[i].length==1)
                        {
                            currentMaximumConsectiveNumbers=1;
                        }
                    
                        System.out.println("This is newly identified streak of consecutive Numbers: " + currentMaximumConsectiveNumbers);
                    
                        if (countIndexLocationsNoZero==(Store[i].length-1))
                        {
                            posZero=Store[i].length-1;   
                        }
                        else
                        {
                            posZero=countIndexLocationsNoZero;
                        }
                    
                        storeMaxConsecutiveSequences = new int [Store[i].length][1][Store[i].length];
                    
                        storeMaxConsecutiveSequences[0][0]= Store[i];
                    
                        countSequencesStored++;
                        
                        System.out.println("Highest consecutive number sequence so far: " + Arrays.toString(Store[i]));
                        break;
                    }
                }
                
                if (zeroFound && posZero==0)
                {
                    j=0;
                }
                
                if (posZero>backupPosZero && posZero>captureIndex && (j==Store[i].length-1 ||j==Store[i].length))
                {
                    posZero=captureIndex;
                }
                    
                if (!hasMoreConsecutive)
                {
                    if ((countIndexLocationsNoZero==currentMaximumConsectiveNumbers)
                    &&countIndexLocationsNoZero+1==currentMaximumConsectiveNumbers  && Store[i][0]==0)
                    {
                        System.out.println("The following: " + Arrays.toString(Store[i]) + 
                        "      has been stored. \nIt is also equal to current maximum sequence of consecutive numbers: " + currentMaximumConsectiveNumbers);
                        storeMaxConsecutiveSequences[countSequencesStored][0]= Store[i];
                    
                        countSequencesStored++;
                        break;
                    }
                    else
                    {
                        if ((j>=posZero &&posZero!=0 && zeroFound && countIndexLocationsNoZero>=posZero && Store[i][j-1]!=0 && j>=Store[i].length
                        || ((countIndexLocationsNoZero==currentMaximumConsectiveNumbers && (j==Store[i].length)
                        ||countIndexLocationsNoZero+1==currentMaximumConsectiveNumbers  && Store[i][0]==0))))
                        {
                            
                            System.out.println("The following: " + Arrays.toString(Store[i]) 
                            + "      has been stored. \nIt is also equal to current maximum sequence of consecutive numbers: " + currentMaximumConsectiveNumbers);
                            storeMaxConsecutiveSequences[countSequencesStored][0]= Store[i];
                    
                            countSequencesStored++;
                            break;
                        }
                    }
                }
                hasMoreConsecutive=false;
            }//end of for
        }//end of for
        
        
        if (Store.length==0)
        {
            System.out.println("No data provided");
        }
        
        else
        {
            if(Store[0].length>1)
            {
                if (storeMaxConsecutiveSequences[0][0][0]==0 && storeMaxConsecutiveSequences[0][0][1]==0)
                {
                    System.out.println("All values in initial array are 0");
                    currentMaximumConsectiveNumbers=1;
                }
            }
            else
            {
                if (currentMaximumConsectiveNumbers==0)
                {
                    System.out.println("Single value in initial array is 0");
                    currentMaximumConsectiveNumbers=1;
                }
            }
        }
        
        System.out.println("\n\n****************************");
        System.out.println("Longest consecutive sequence: " + currentMaximumConsectiveNumbers);
        System.out.println("****************************");
        
        int counter=0;
        
        for (int[][] sequences : storeMaxConsecutiveSequences)
        {
            System.out.println(Arrays.deepToString(sequences));
            
            counter++;
        }
    }//end of main
}//end of class